<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>personal blog | Log in</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" th:href="@{/admin/dist/img/favicon.png}"/>
    <!-- Font Awesome -->
    <link rel="stylesheet" th:href="@{/admin/dist/css/font-awesome.min.css}">
    <!-- Ionicons -->
    <link rel="stylesheet" th:href="@{/admin/dist/css/ionicons.min.css}">
    <!-- Theme style -->
    <link rel="stylesheet" th:href="@{/admin/dist/css/adminlte.min.css}">
    <style>
        canvas {
            display: block;
            vertical-align: bottom;
        }

        #particles {
            background-color: #F7FAFC;
            position: absolute;
            top: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
        }
    </style>
</head>
<body class="hold-transition login-page">
<div id="particles">
</div>
<div class="login-box">
    <div class="login-logo" style="color: #007bff;">
        <h1>personal blog</h1>
    </div>
    <!-- /.login-logo -->
    <div class="card">
        <div class="card-body login-card-body">
            <p class="login-box-msg">your personal blog , enjoy it</p>
            <form id="loginForm" onsubmit="return false">
                <div class="form-group">
                    <div id="danger" class="alert alert-danger" hidden></div>
                </div>
                <div class="form-group has-feedback">
                    <span class="fa fa-user form-control-feedback"></span>
                    <input type="text" id="userName" name="userName" class="form-control" placeholder="请输入账号"
                           required="required">
                </div>
                <div class="form-group has-feedback">
                    <span class="fa fa-lock form-control-feedback"></span>
                    <input type="password" id="password" name="password" class="form-control" placeholder="请输入密码"
                           required="required">
                </div>
                <div class="row">
                    <div class="col-6">
                        <input type="text" id="verifyCode" class="form-control" name="verifyCode" placeholder="请输入验证码"
                               required="required">
                    </div>
                    <div class="col-6">
                        <img alt="单击图片刷新！" class="pointer" style="height: 35px;width: 150px;border-radius: 25px"
                             onclick="refreshImg()">
                    </div>
                </div>
                <div class="form-group has-feedback"></div>
                <div class="row">
                    <div class="col-8">
                    </div>
                    <div class="col-4">
                        <button id="btn_login" class="btn btn-primary btn-block btn-flat">登录
                        </button>
                    </div>
                </div>
            </form>

        </div>
        <!-- /.login-card-body -->
    </div>
</div>
<!-- /.login-box -->

<!-- jQuery -->
<script th:src="@{/admin/dist/js/jquery/jquery-3.5.1.min.js}"></script>

<script>
    $(function () {
        // $(".alert-danger").hide();
        refreshImg();
    });

    function refreshImg() {
        let p = {p: new Date()};
        $.ajax({
            url: "/admin/kaptcha",
            type: "post",
            contentType: "application/json",
            success: function (data) {
                // console.info("image=" + JSON.stringify(data));
                let s = 'data:image/jpeg;base64,' + data.data.img;
                $(".pointer").attr('src', s);
            }
        })
    }

    $("#btn_login").click(function () {
        let userName = $("#userName").val();
        let password = $("#password").val();
        let verifyCode = $("#verifyCode").val();
        $("#danger").show();

        let data = {username: userName, password: password, verifyCode: verifyCode};
        $.ajax({
            url: "/admin/logins",
            type: "post",
            contentType: "application/json",
            data: JSON.stringify(data),
            success: function (data) {
                console.info(data)
                if (data.code === 1000) {
                    window.location.href = "/admin/index";
                } else {
                    console.info(data.msg);
                    let alert = $(".alert-danger");
                    alert.removeAttr("hidden");
                    alert.text(data.msg);
                }
            }
        })
    })
</script>
</body>
</html>
